<?php


namespace app\api\controller\v16;

use app\api\controller\v2\Auth;
use app\common\cachemodel\ArticleComment;
use app\common\cachemodel\ArticleCommentGood;
use app\common\cachemodel\ArticleCommentReply;
use app\common\cachemodel\ForbidenRecord;
use app\common\cachemodel\User;
use app\common\cachemodel\ZlAdminVip;
use app\common\cachemodel\ZlUserNoComment;
use app\common\lib\Util;
use app\common\model\ArticleGood;
use app\common\model\Informant;
use app\common\model\MiddleInformant;
use think\Cache;
use think\Db;
use think\Log;

/**
 * 举报，点赞，收藏类
 * @version
 * Class ContentDispose
 * @package app\api\controller\v16
 */
class ContentDispose extends Auth
{
    /**
     * 内容举报
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function complain()
    {
        $id = input('id/d', 0);//内容id
        $type = input('type/d', 0);//[1=>评论，2=>回复，3=》视频评价，4=>问答问题, 5=>星座讨论问题 6=>星运变动动态]
        $c_id = input('c_id/d', 0);//举报id
        $isAuth = input('is_ask/d', 0);//1=>验证内部用户操作

        //参数校验
        if (!in_array($type, [1, 2, 3, 4, 5, 6], false))
        {
            return show(config('code.error'), '未知的数据');
        }
        //验证是否重复操作
        if (Util::commentCheck($this->user['id'], ['id' => $id, 'type' => $type, 'c_id' => $c_id]))
        {
            return show(config('code.error'), '别心急，等一等');
        }
        //获取举报信息
        $informantInfo = (new Informant())->getInfoById($c_id, ['content']);
        if (!$informantInfo)
        {
            return show(config('code.error'), '举报异常，请稍后再试');
        }

        //与数据库字段进行匹配
        if ($type === 2)
        {
            $type = 3;//回复
        } elseif ($type === 3)
        {
            $type = 2;//视频评价
        }

        if ($isAuth)
        {
            $commentModel = new ArticleComment();
            //若是内部vip3进行举报，并且是举报的评论和回复，则进行相关操作
            $vip3 = (new ZlAdminVip())->getVipInfo(3);
            try
            {
                if (in_array($this->user['id'], $vip3) && in_array($c_id, [1, 2, 3, 4], true) && in_array($type, [1, 3], true))
                {
                    switch ($c_id)
                    {
                        case 1:
                            // 删除当前评论
                            $res = $commentModel->deleteComment($this->user['id'], $id, $c_id, $type);
                            break;
                        case 2: // 删除所有评论和评论回复
                            $res = $commentModel->deleteCommentAll($this->user['id'], $id, $c_id, $type);
                            break;
                        case 3: // 删除所有评论和评论回复并禁止评论
                            $res = $commentModel->deleteCommentAll($this->user['id'], $id, $c_id, $type);
                            // 文章评论禁言
                            $noCommentData = [
                                'uid' => $res,//被禁言的用户
                                'atype' => 1,
                                'aid' => $this->user['id'],
                                'create_time' => time()
                            ];
                            (new ZlUserNoComment())->save($noCommentData);
                            Cache::rm('userinfo:' . $res);
                            break;
                        case 4: // 删除所有评论和评论回复并封号
                            $res = $commentModel->deleteCommentAll($this->user['id'], $id, $c_id, $type);
                            // 封号
                            (new User())->save(['forbidden' => 1], ['id' => $res]);
                            //添加封号记录
                            (new ForbidenRecord())->saveData($res, $this->user['id'], $informantInfo['content']);
                            Cache::rm('userinfo:' . $res);
                            break;
                        default :
                            $res = false;
                    }
                    if ($res)
                    {
                        return show(config('code.success'), '操作成功');
                    }
                    return show(config('code.error'), '操作失败');
                }
            }
            catch (\Exception $e)
            {
                Log::write('内部用户 ' . $this->user['id'] . '举报失败：' . $e->getMessage(), 'error');
            }
        }

        switch ($type)
        {
            case 1://评论
            case 3://回复
            case 4://问答问题
            case 5://星座讨论
            case 6://星运变动动态
                $arr = [
                    'comment_id' => $id,
                    'info_id' => $c_id,
                    'uid' => $this->user['id'],
                    'create_time' => time(),
                    'type' => $type,
                ];
                $res = (new MiddleInformant())->SaveData($arr);
                if ($res === false)
                {
                    return show(config('code.error'), '举报失败');
                }
                break;
            //视频评论
            case 2:
                $array = [
                    'uid' => $this->user['id'],
                    'cid' => $id,
                    'content' => $c_id,
                    'create_time' => time()
                ];

                try
                {
                    Db::table('wechat_backup_07_18_14_43_00.zhunle_course_comment_complain')->insertGetId($array);
                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '投诉失败');
                }
                break;
        }
        return show(config('code.success'), '投诉成功');
    }

    /**
     * 点赞/取消点赞(内容)
     * @throws \Exception
     */
    public function content_good()
    {
        $id = input('id/d', 0);//内容id
        $type = input('type/d', 0);//内容类型 [1=>星文，2=>女神动态，3=>女神专区]
        //验证是否重复操作
        if (Util::commentCheck($this->user['id'], ['id' => $id, 'type' => $type]))
        {
            return show(config('code.error'), '别心急，等一等');
        }
        $articleGoodModel = new ArticleGood();
        //获取用户已点赞的数据
        $goddessGood = $articleGoodModel->getGoodFromCache($this->user['id'], $type);
        //更新文章点赞数据
        if (in_array($id, $goddessGood))
        {
            //取消点赞
            $articleGoodModel->delData($this->user['id'], $id, ['aid' => $id, 'uid' => $this->user['id'], 'type' => $type], $type);
            return show(config('code.success'), '取消点赞成功');
        }

        //添加点赞
        $articleGoodModel->saveData($this->user, $id, ['aid' => $id, 'uid' => $this->user['id'], 'create_time' => time(), 'type' => $type], $type);
        return show(config('code.success'), '点赞成功');
    }

    /**
     * 点赞/取消点赞(评论/回复)
     * @throws \Exception
     */
    public function comment_good()
    {
        $id = input('id/d', 0);
        $type = input('type/d', 0);//[1=>评论，2=>回复]
        //验证是否重复操作
        if (Util::commentCheck($this->user['id'], ['id' => $id, 'type' => $type]))
        {
            return show(config('code.error'), '别心急，等一等');
        }
        $articleCommentGood = new ArticleCommentGood();
        //获取用户已点赞的数据,并更新评论点赞数据
        $goddessGood = $articleCommentGood->getGoodFromCache($this->user['id'], $type);
        if (in_array($id, $goddessGood))
        {
            //取消点赞
            $articleCommentGood->delData($this->user['id'], $id, ['comment_id' => $id, 'uid' => $this->user['id'], 'type' => $type], $type);
            return show(config('code.success'), '取消点赞成功');
        }
        //添加点赞
        $articleCommentGood->saveData($this->user, $id, ['comment_id' => $id, 'uid' => $this->user['id'], 'create_time' => time(), 'type' => $type], $type);
        return show(config('code.success'), '点赞成功');
    }

    /**
     * 删除评论/回复
     * @throws \Exception
     */
    public function del()
    {
        $id = input('id/d', 0);
        $type = input('type/d', 0);//[1=>评论，2=>回复]
        //验证是否重复操作
        if (Util::commentCheck($this->user['id'], ['id' => $id, 'type' => $type]))
        {
            return show(config('code.error'), '别心急，等一等');
        }
        if ($type === 1)
        {
            //删除评论
            (new ArticleComment())->commentDel($this->user['id'], $id);
        } elseif ($type === 2)
        {
            //删除回复
            (new ArticleCommentReply())->delReply($this->user['id'], $id);
        } else
        {
            return show(config('code.error'), '未知的信息');
        }
        return show(config('code.success'), '删除成功');
    }
}
